package com.tma.timesheettracker.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

import com.tma.timesheettracker.R;
import com.tma.timesheettracker.contentprovider.TimeTrackerContentProvider;
import com.tma.timesheettracker.model.Task;
import com.tma.timesheettracker.model.TrackingEntry;
import com.tma.timesheettracker.util.Util;

public class PreviousActivity extends Activity implements OnClickListener {

	public static final int REQUEST_CODE = 2222;
	private List<TrackingEntry> m_lstPreEntry = new ArrayList<TrackingEntry>();
	private int ahourSize = 0;
	private ContentResolver m_resolver;
	private LinearLayout m_lnPreviousDetail;
	private Date m_dayTracking;
	private LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
			LinearLayout.LayoutParams.MATCH_PARENT,
			LinearLayout.LayoutParams.WRAP_CONTENT);

	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		m_resolver = getContentResolver();

		m_lstPreEntry = TimeTrackerContentProvider.TrackingEntryTable
				.getEntries(m_resolver, Util.formatDate(Util.getPreviousDay()));

		setContentView(R.layout.fragment_detail);
		m_lnPreviousDetail = (LinearLayout) findViewById(R.id.lnPreviousDetail);
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			ahourSize = bundle.getInt(Util.ONE_HOUR_SIZE);
			m_lstPreEntry = (List<TrackingEntry>) bundle
					.getSerializable(Util.TIME_SHEET_TRACKING);
		}

		getPreviousDay(ahourSize);

		getActionBar().setHomeButtonEnabled(true);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private void getPreviousDay(int hour) {
		layoutParams.setMargins(10, 10, 10, 0);
		m_lnPreviousDetail.removeAllViews();
		if (m_lstPreEntry.size() > 0) {
			m_dayTracking = m_lstPreEntry.get(0).getDate();
			setTitle(Util.getDateFormat(m_lstPreEntry.get(0).getDate()));
			int count = 0;
			for (TrackingEntry entry : m_lstPreEntry) {
				if (entry.getDuration() != 0) {
					Task task = TimeTrackerContentProvider.TaskTable.getTask(
							m_resolver, entry.getTask_id());
					createButtonAndAddToView(entry.getDuration(), hour,
							R.color.green, task.getId(), task.getName());
					count += entry.getDuration();
				}
			}
			int notlog = Util.WORKING_HOURS - count;
			if (notlog != 0) {
				createButtonAndAddToView(notlog, hour, R.color.red,
						Util.CREATE, "+");
			}
		} else {
			createButtonAndAddToView(Util.WORKING_HOURS, hour, R.color.red,
					Util.CREATE, "+");
			m_dayTracking = Util.getPreviousDay();
		}
	}

	private void createButtonAndAddToView(int time, int hourSize, int color,
			Object tag, String name) {
		Button btn = new Button(this);
		btn.setHeight(time * hourSize);
		btn.setOnClickListener(this);
		btn.setText(name);
		btn.setTag(tag);
		btn.setLayoutParams(layoutParams);
		btn.setBackgroundColor(this.getResources().getColor(color));
		if (R.color.green == color) {
			btn.setGravity(Gravity.START);
		}
		m_lnPreviousDetail.addView(btn);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			onBackPressed();
			return true;
		case R.id.addTask:
			Intent intent = new Intent(PreviousActivity.this,
					RecordWorkActivity.class);
			intent.putExtra(Util.ACTION, Util.CREATE);
			intent.putExtra(Util.TIME_SHEET_TRACKING,
					(Serializable) m_lstPreEntry);
			intent.putExtra(Util.DAY_TO_RECORD, Util.dfm.format(m_dayTracking));
			startActivityForResult(intent, RecordWorkActivity.REQUEST_CODE);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void onClick(View v) {
		Button btn = (Button) v;
		if (btn.getTag().equals(Util.CREATE)) {
			Intent intent = new Intent(PreviousActivity.this,
					RecordWorkActivity.class);
			intent.putExtra(Util.ACTION, Util.CREATE);
			intent.putExtra(Util.TIME_SHEET_TRACKING,
					(Serializable) m_lstPreEntry);
			intent.putExtra(Util.DAY_TO_RECORD, Util.dfm.format(m_dayTracking));
			startActivityForResult(intent, RecordWorkActivity.REQUEST_CODE);
		} else {
			Intent intent = new Intent(PreviousActivity.this,
					RecordWorkActivity.class);
			intent.putExtra("Task_Name", btn.getText().toString());
			intent.putExtra("Task_Id", btn.getTag().toString());
			intent.putExtra(Util.ACTION, Util.EDIT);
			intent.putExtra(Util.TIME_SHEET_TRACKING,
					(Serializable) m_lstPreEntry);
			intent.putExtra(Util.DAY_TO_RECORD, Util.dfm.format(m_dayTracking));
			startActivityForResult(intent, RecordWorkActivity.REQUEST_CODE);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == RecordWorkActivity.REQUEST_CODE
				&& resultCode == Util.ACTION_CLOSE) {
			setResult(Util.ACTION_CLOSE);
			finishActivity(REQUEST_CODE);
			finish();
		}
	}
}
